package com.healthtop.web;

import com.google.common.collect.Lists;
import com.healthtop.common.HttpMessageResult;
import com.healthtop.common.Views;
import com.healthtop.dto.*;
import com.healthtop.model.XsRole;
import com.healthtop.model.XsUser;
import com.healthtop.service.DIYService;
import com.healthtop.service.NavigationService;
import com.healthtop.service.XsUserService;
import com.healthtop.utils.AjaxResponseStatus;
import com.healthtop.utils.DateUtil;
import com.healthtop.utils.UserUtils;
import com.healthtop.utils.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by Peter on 2017/9/12.
 */
@Controller
public class AdminController {

    @Autowired
    private XsUserService userService;

    @Autowired
    private NavigationService navigationService;

    @Autowired
    private DIYService diyService;


    @RequestMapping("/index")
    public String index(){
        return Views.INDEX;
    }

    @RequestMapping("/")
    public String redirectIndex(){
        return Views.FORWARD_SITES_HOME_PAGE;
    }

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return Views.LOGIN;
    }


    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(LoginDto loginDto, HttpServletRequest request){
        XsUser user = userService.login(loginDto,request);
        if(loginDto.isError()){
            return Views.LOGIN;
        }else{
            List<MenuDto> menus = navigationService.getMenus(user.getUserId());
            UserUtils.setMenus(menus);
            return Views.INDEX;
        }
    }


    @RequestMapping(value="/logout")
    public String logout(){
        UserUtils.cleanSession();
        return Views.REDIRECT_LOGIN;
    }


    /* 获取验证码图片*/

    @RequestMapping(value="/getVerifyCode")
    public void getVerificationCode(HttpServletResponse response, HttpServletRequest request) {

        try {
                        
            int width=200;

            int height=69;

            BufferedImage verifyImg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

//生成对应宽高的初始图片

            String randomText = VerifyCode.drawRandomText(width,height,verifyImg);

//单独的一个类方法，出于代码复用考虑，进行了封装。

//功能是生成验证码字符并加上噪点，干扰线，返回值为验证码字符

            request.getSession().setAttribute("verifyCode", randomText);

            response.setContentType("image/png");//必须设置响应内容类型为图片，否则前台不识别

            OutputStream os = response.getOutputStream(); //获取文件输出流

            ImageIO.write(verifyImg,"png",os);//输出图片流

            os.flush();

            os.close();//关闭流

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

    @RequestMapping(value="/toRegister")
    public String toRegister(){
        return Views.TO_TEGISTER;
    }

    @RequestMapping(value = "/registered",method = RequestMethod.POST)
    public String login(RegisteredDto registeredDto, HttpServletRequest request){
        XsUser user = userService.registered(registeredDto,request);
        if(registeredDto.isError()){
            return Views.TO_TEGISTER;
        }else{
            List<MenuDto> menus = navigationService.getMenus(user.getUserId());
            UserUtils.setMenus(menus);
            return Views.INDEX;
        }
    }

    @RequestMapping("/updatePwd")
    public String updatePwd(XsUserDto xsUserDto){
        userService.updatePwd(xsUserDto);
        return Views.UPDATE_PASSWORD;
    }

    @RequestMapping("/updatePassword")
    @ResponseBody
    public AjaxResponseStatus updatePassword(XsUserDto xsUserDto){
      return   userService.updatePassword(xsUserDto);
    }
}
